﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IRProject.Model
{
    class AndNode : Node
    {
        public override List<ResultData> GetResult()
        {
            List<ResultData> resultlist = new List<ResultData>();
            foreach (Node n in Children)
            {
                List<ResultData> temp = n.GetResult();
                foreach (ResultData r in temp)
                {
                    resultlist = marge(resultlist, temp);
                }
            }
            return resultlist;
        }

        List<ResultData> marge(List<ResultData> listA, List<ResultData> listB)
        {
            if (listA.Count == 0)
                return listB;
            if (listB.Count == 0)
                return listA;

            List<ResultData> list = new List<ResultData>();
            foreach (ResultData ra in listA)
                foreach (ResultData rb in listB)
                {
                    if (ra.DocId == rb.DocId && !list.contain(ra.DocId))
                    {
                        ResultData data = operate(ra, rb);
                        list.Add(data);
                    }
                }
            return list;
        }

        private ResultData operate(ResultData ra, ResultData rb)
        {
            ResultData data = new ResultData();
            data.DocId = ra.DocId;
            data.Similarity = ra.Similarity + rb.Similarity;
            return data;
        }

    }
}
